package cn.edu.lit.jsjxy.CourseExample.chapter2;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import cn.edu.lit.jsjxy.CourseExample.R;

public class ImageTestActivity extends Activity implements View.OnClickListener, View.OnTouchListener
{

    int[] images = new int[]{R.drawable.ninja151446125142171,
            R.drawable.ninja151446130645937, R.drawable.ninja151446132661988,
            R.drawable.ninja151446137521683, R.drawable.ninja151446141624703,
            R.drawable.ninja151446142841855, R.drawable.ninja151446147277656};

    ImageSwitcher ims;
    Button btnPrev, btnNext, btnPrev1, btnNext1;
    ImageView iv1;

    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_test);

        ims = findViewById(R.id.ims1);
        btnPrev = findViewById(R.id.btnPrev);
        btnNext = findViewById(R.id.btnNext);
        btnPrev1 = findViewById(R.id.btnPrev1);
        btnNext1 = findViewById(R.id.btnNext1);
        iv1 = findViewById(R.id.iv1);

        iv1.setBackgroundColor(Color.RED);


        btnNext.setOnClickListener(this);
        btnPrev.setOnClickListener(this);
        btnNext1.setOnClickListener(this);
        btnPrev1.setOnClickListener(this);


        ims.setInAnimation(this, android.R.anim.fade_in);//设置淡入的动画
        ims.setOutAnimation(this, android.R.anim.fade_out);//设置淡出的动画

        ims.setFactory(new ViewSwitcher.ViewFactory()
        {

            @Override
            public View makeView()
            {
                ImageView imageView = new ImageView(ImageTestActivity.this);//创建一个Iv 的类
                imageView.setAdjustViewBounds(true);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置保持横纵比居中缩放图片
                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(400, 300));
                imageView.setBackgroundColor(Color.BLUE);
                return imageView;
            }
        });

        ims.setImageResource(images[index]);
        ims.setOnClickListener(this);

    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId()) {
            case R.id.btnPrev:
                if (index > 0) {

                    index--;

                } else {
                    index = images.length - 1;
                }
                ims.setImageResource(images[index]);//显示当前的图片


                break;

            case R.id.btnNext:
                if (index < images.length - 1) {
                    index++;

                } else {
                    index = 0;
                }
                ims.setImageResource(images[index]);

                break;

            case R.id.btnPrev1:
                if (index > 0) {

                    index--;

                } else {
                    index = images.length - 1;
                }
                iv1.setImageResource(images[index]);//显示当前的图片


                break;

            case R.id.btnNext1:
                if (index < images.length - 1) {
                    index++;

                } else {
                    index = 0;

                }
                iv1.setImageResource(images[index]);


                break;
        }

    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent)
    {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return true;
    }
}
